<script lang="ts" setup>
// #region 引入包, 函数和组件
import { useFoodStore } from '@/stores/counter'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import type { FoodsListItem } from '@/api' // 类型
// #endregion

// #region // 使用模块(仓库、路由、父组件参数函数)
const foodStore = useFoodStore()
const route = useRoute()
// #endregion

// #region // 变量
const food = ref<FoodsListItem>()
// #endregion

// #region // 函数

// #endregion

// #region // 生命周期和vue函数
watch(
  () => route.query.name,
  (newVal) => {
    if (newVal !== '' && typeof newVal === 'string') {
      food.value = foodStore.queryFood(newVal)
    }
  },
  { immediate: true },
)
// #endregion
</script>

<template>
  <div class="detail">
    <header>
      <a href="javascript:void(0);" @click="$router.back">&lt;</a>
      <h3>{{ food?.name }}</h3>
    </header>
    <head-controls></head-controls>
    <img :src="food?.imgUrl" />
    <p>升糖指数: {{ food?.glycemicIndex }}</p>
    <p>{{ food?.content }}</p>
  </div>
</template>

<style lang="scss" scoped>
.detail {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  overflow-y: auto;
  header {
    height: 50rem;
    width: 100%;
    line-height: 50rem;
    font-size: 24rem;
    color: var(--c-b);
    padding-left: 10rem;
    display: flex;
    text-align: center;
    position: sticky;
    justify-content: center;
    border-bottom: 1rem solid #f4f4f4;
    top: 0;
    left: 0;
    background: #fff;
    a {
      font-size: 24rem;
      margin-right: 10rem;
      transform: scale(0.8, 1.4);
      position: fixed;
      left: 10rem;
      top: 0;
      color: var(--c-b);
    }
  }
  img {
    width: 100%;
    height: 300rem;
    object-fit: cover;
  }
  p {
    padding: 20rem;
  }
}
</style>
